package roslan.musicbox.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class StatusBar extends JPanel{
	private static final long serialVersionUID = 7861479655764345429L;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JProgressBar progressBar;
	private JLabel hintLabel;
	
	private int showTime;
	private Timer showTimer;
	private TimerTask showTask;
	
	private MusicLibPanel musicLibPanel;
	
	public StatusBar(MusicLibPanel panel){
		this.musicLibPanel = panel;
	}
	
	public void init(){
		leftPanel = new JPanel();
		rightPanel = new JPanel();
		progressBar = new JProgressBar();
		hintLabel = new JLabel();
		
		showTime = 5000;
		showTask = new TimerTask(){
			public void run(){
				if(hintLabel != null){
					hintLabel.setVisible(false);
				}
			}
		};
		showTimer = new Timer();
	}
	
	public void setLayout(){
		super.setLayout(new BorderLayout());
		super.setBorder(new LineBorder(new Color(149,152,148), 1));
		
		leftPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,3));
		leftPanel.add(hintLabel);
		
		progressBar.setPreferredSize(new Dimension(160,18));
		progressBar.setValue(50);
		progressBar.setBorder(null);
		progressBar.setVisible(false);
		rightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,5,3));
		rightPanel.add(progressBar);
		
		this.setPreferredSize(new Dimension(300, 24));
		this.add(leftPanel, "Center");
		this.add(rightPanel, "East");
	}
	
	public void setAction(){
		
	}
	
	public void watch(final ProgressDetectable task){
		setProgress(0);
		progressBar.setVisible(true);
		Thread thread = new Thread(){
			public void run(){
				while(!task.isTaskOver()){
					final int progress = task.getProgress();
					final String info = task.getInfo();
					SwingUtilities.invokeLater(
							new Runnable(){
								public void run(){
									setProgress(progress);
									setHint(info);
								}
							}
					);
					try{
						Thread.sleep(100);
					}catch(Exception e){}
				}
				try {
					SwingUtilities.invokeAndWait(
							new Runnable(){
								public void run(){
									setProgress(100);
									setHint(task.getInfo());
								}
							}
					);
				} catch(Exception e){}
				try{
					Thread.sleep(showTime);
				}catch(Exception e){}
				SwingUtilities.invokeLater(
						new Runnable(){
							public void run(){
								progressBar.setVisible(false);
								repaint();
								musicLibPanel.repaint();
							}
						}
				);
			}
		};
		thread.start();
	}
	
	public void setShowTime(int showTime){
		if(showTime < 0 || showTime > 1000000){
			return;
		}
		this.showTime = showTime;
	}
	
	public void setProgressBarVisible(boolean visible){
		if(progressBar == null) return;
		progressBar.setVisible(visible);
		repaint();
	}
	
	public void setHint(String hint){
		if(hintLabel == null) return;
		showTask.cancel();
		showTask = new TimerTask(){
			public void run(){
				if(hintLabel != null){
					hintLabel.setVisible(false);
					repaint();
				}
			}
		};
		showTimer.schedule(showTask, showTime);
		hintLabel.setText(hint);
		hintLabel.setVisible(true);
	}
	
	public void setProgress(int value){
		if(value < 0 || value > 100) return;
		progressBar.setValue(value);
	}
}
